package sample;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import sample.entity.Message;

public class ListMessagesProgramatical {
	public static void main(String[] args) {

		Configuration conf = new Configuration();
//		conf.addAnnotatedClass(Message.class);
		conf.addAnnotatedClass(Message.class);
		conf.setProperty("hibernate.connection.driver_class", "org.gjt.mm.mysql.Driver");
		conf.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/apress_hibernate");
		conf.setProperty("hibernate.connection.username", "learner");
		conf.setProperty("hibernate.connection.password", "learnerpw");
		conf.setProperty("hibernate.show_sql", "0");
		conf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
		
		SessionFactory factory = conf.buildSessionFactory();

		Session session = factory.openSession();

		List messages = session.createQuery("from Message").list();
		System.out.println("Found " + messages.size() + " message(s):");

		Iterator i = messages.iterator();
		while (i.hasNext()) {
			Message msg = (Message) i.next();
			System.out.println(msg.getMessageText());
		}

		session.close();
	}
}